using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using RandomRat.Data;

namespace RandomRat {

	public enum CriteriaNameDialogType { SaveAs, Rename };

	/// <summary>
	/// Summary description for CriteriaName.
	/// </summary>
	public class CriteriaName : System.Windows.Forms.Form {

		CriteriaDataSet criteriaDataSet;

		private System.Windows.Forms.Button butCancel;
		private System.Windows.Forms.Button butOK;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.Container components = null;

		public CriteriaName(CriteriaDataSet criteriaDataSet, CriteriaNameDialogType dialogType) {
			constructorHelper(criteriaDataSet, null, dialogType);
		}
		public CriteriaName(CriteriaDataSet criteriaDataSet, string criteriaID, CriteriaNameDialogType dialogType) {
			constructorHelper(criteriaDataSet, criteriaID, dialogType);
		}
		private void constructorHelper(CriteriaDataSet criteriaDataSet, string criteriaID, CriteriaNameDialogType dialogType) {
			InitializeComponent();
			
			if (dialogType == CriteriaNameDialogType.SaveAs) {
				this.Text = "Save Criteria As";
			} else if (dialogType == CriteriaNameDialogType.Rename) {
				this.Text = "Rename Criteria";
			}
			
			this.criteriaDataSet = criteriaDataSet;

			textBox1.Focus();
			if (criteriaID != null) {
				textBox1.Text = criteriaID;
			}
		}


		public string CriteriaID {
			get { return textBox1.Text; }
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.butCancel = new System.Windows.Forms.Button();
			this.butOK = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// butCancel
			// 
			this.butCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.butCancel.Location = new System.Drawing.Point(232, 26);
			this.butCancel.Name = "butCancel";
			this.butCancel.Size = new System.Drawing.Size(56, 20);
			this.butCancel.TabIndex = 5;
			this.butCancel.Text = "Cancel";
			// 
			// butOK
			// 
			this.butOK.Location = new System.Drawing.Point(171, 26);
			this.butOK.Name = "butOK";
			this.butOK.Size = new System.Drawing.Size(56, 20);
			this.butOK.TabIndex = 4;
			this.butOK.Text = "OK";
			this.butOK.Click += new System.EventHandler(this.butOK_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 6;
			this.label1.Text = "Name:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 26);
			this.textBox1.MaxLength = 256;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(156, 20);
			this.textBox1.TabIndex = 7;
			this.textBox1.Text = "";
			// 
			// CriteriaName
			// 
			this.AcceptButton = this.butOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.butCancel;
			this.ClientSize = new System.Drawing.Size(296, 54);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.textBox1,
																		  this.label1,
																		  this.butCancel,
																		  this.butOK});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "CriteriaName";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Criteria Name";
			this.Activated += new System.EventHandler(this.CriteriaName_Activated);
			this.ResumeLayout(false);

		}
		#endregion

		private void butOK_Click(object sender, System.EventArgs e) {
			bool selectionValid = true;
			


			if (textBox1.Text.Trim() == "") {
				MessageBox.Show(this, "Please provide a name for the criteria");
				selectionValid = false;
				textBox1.Focus();
				textBox1.SelectAll();

			} else if (criteriaIDExists()) {
				if (MessageBox.Show(this.Parent, "Criteria of this name already exists." + "\n" +
					"Do you want to replace it?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) {
					selectionValid = false;
					textBox1.Focus();
					textBox1.SelectAll();
				}																	
			 
			}

			if (selectionValid) {
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}

		private bool criteriaIDExists() {
			string criteriaID = textBox1.Text.Trim();
			DataTable criteria = criteriaDataSet.Tables["Criteria"];
			
			for (int i = 0; i < criteria.Rows.Count; i++) {
				if (criteriaID.CompareTo(criteria.Rows[i]["CriteriaID"]) == 0) {
					return true;
				}
			}

			return false;
		}


		private void CriteriaName_Activated(object sender, System.EventArgs e) {
			textBox1.Focus();
			textBox1.SelectAll();
		}
	}
}
